<?php
namespace Core\CoreComponents\AccountingManager;
use Core;

require_once(COMPONENTS_DIR . 'AccountingManager/IAccounts.php');
require_once(CONTRACTS_DIR . 'account.entity.php');

/**
* This interface provide all accessible methods on Account module
*
* @method ServiceResponse getAccounts()
* @method ServiceResponse addAccount($state, $name)
*/
class Accounts implements IAccounts
{
	/**
	 * Add an account
	 *
	 * @param integer $state The account status
	 * @param string $name The new account name
	 *
	 * @return ServiceResponse This response contains the added account
	 */
	public function addAccount($state, $name)
	{
		Core\CoreCommons\Logger::Info("addAccount $state $name");
		
		$response = null;
		
		try
		{	
			$session = Core\Datastorage\Session::Instance();
			$session->Open();
			
			$account = Core\CoreContracts\Account::Create($state, $name);
			
			$result = $session->Save($account);
			$response = new Core\CoreCommons\ServiceResponse($result);

			$session->Close();
			Core\CoreCommons\Logger::Info("Account $state $name added");
		}
		catch (Exception $ex) 
		{
			$response = Core\CoreCommons\ServiceResponse::CreateError($ex);
		}
		
		return $response;
	}
	
	/**
	 * Get all accounts
	 * 
	 * @return ServiceResponse This response contains all accounts
	 */
    public function getAccounts()
	{
		Core\CoreCommons\Logger::Info("getAccounts");
		
		$response = null;
		
		try
		{	
			$session = Core\Datastorage\Session::Instance();
			$session->Open();
		
			$session->CreateCriteria('Account');
			$result = $session->ListResult();
			
			$response = new Core\CoreCommons\ServiceResponse($result);
			
			$session->Close();
			
			Core\CoreCommons\Logger::Info("getAccounts");
		}
		catch (\Exception $ex) 
		{
			$response = Core\CoreCommons\ServiceResponse::CreateError($ex);
		}

		return $response;
	}
	
	/**
	 * Update an account
	 *
	 * @param integer $id The unique identifier of the account
	 * @param string $name The new name account
	 * @param integer $state The new state account
	 *
	 * @return ServiceResponse This response contains the updated account 
	 */
	public function updateAccount($id, $name, $state)
	{
		Core\CoreCommons\Logger::Info("Service - updateAccount $id, $name, $state");
		
		$response = null;
		
		try
		{	
			$session = Core\Datastorage\Session::Instance();
			$session->Open();
			
			$account = $session->Load('Account', $id);
			
			$account->Name = $name;
			$account->State = $state;
			$account->ModificationDate = date("Y-m-d H:i:s");
			
			$result = $session->Update($account);
			$response = new Core\CoreCommons\ServiceResponse($result);

			$session->Close();
			Core\CoreCommons\Logger::Info("Service - Account $id, $name, $state updated");
		}
		catch (Exception $ex) 
		{
			$response = Core\CoreCommons\ServiceResponse::CreateError($ex);
		}
		
		return $response;
	}
	
	/**
	 * Delete an account
	 * 
	 * @todo This method should take the account id instead of account name
	 * @param string $name The account name 
	 *
	 * @return ServiceResponse This response contains the deleted account 
	 */
	public function deleteAccount($name)
	{
		Core\CoreCommons\Logger::Info("deleteAccount $name");
		
		$response = null;
		
		try
		{	
			$session = Core\Datastorage\Session::Instance();
			$session->Open();
			
			$session->CreateCriteria('Account');
			$session->AddCriteria(Expression::SQL_EQ('Name', $name));
			$account = $session->UniqueResult();
			
			$result = $session->Delete($account);
			$response = new Core\CoreCommons\ServiceResponse($result);

			$session->Close();
			Core\CoreCommons\Logger::Info("Account $name deleted");
		}
		catch (Exception $ex) 
		{
			$response = Core\CoreCommons\ServiceResponse::CreateError($ex);
		}
		
		return $response;
	}
	
	/**
	 * Get an account by name
	 * 
	 * @todo This method should take the account id instead of account name
	 * @param string $name The account name
	 *
	 * @return ServiceResponse This response contains the deleted account 
	 */
	public function getAccount($name)
	{
		Core\CoreCommons\Logger::Info("getAccount $name");
		
		$response = null;
		
		try
		{	
			$session = Core\Datastorage\Session::Instance();
			$session->Open();
			
			$session->CreateCriteria('Account');
			$session->AddCriteria(Expression::SQL_EQ('Name', $name));
			$account = $session->UniqueResult();
			
			if($account == null)
			{
				throw new \Exception("There is no account for this name : $name.");
			}
			
			$response = new Core\CoreCommons\ServiceResponse($account);

			$session->Close();
			Core\CoreCommons\Logger::Info("Get account $name completed");
		}
		catch (Exception $ex) 
		{
			$response = Core\CoreCommons\ServiceResponse::CreateError($ex);
		}
		
		return $response;
	}
}

?> 